package com.lw;

import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.awt.image.BufferedImage;

/**
 * 马里奥
 */
@NoArgsConstructor
public class Mario implements Runnable {
    @Getter
    @Setter
    private int x, y;//坐标

    @Getter
    @Setter
    private String status;//状态

    @Getter
    @Setter
    private BufferedImage show;

    @Setter
    private int ySpeed = 0;//跳跃速度

    @Setter
    private int xSpeed = 0;//移动速度

    @Setter
    private Background background = new Background();

    private int index = 0;

    private int upTime = 0;//跳跃上升时间 单位

    private Thread thread;

    public Mario(int x, int y) {
        this.x = x;
        this.y = y;
        show = StaticValue.stand_R;
        this.status = "stand_right";
        thread = new Thread(this);
        thread.start();
    }

    /**
     * 向左移动
     */
    public void leftMove() {
        xSpeed = -20;
        if (status.indexOf("jump") != -1) {//在跳跃
            status = "jump_left";
        } else {
            status = "move_left";
        }
    }

    /**
     * 向右移动
     */
    public void rightMove() {
        xSpeed = 20;
        if (status.indexOf("jump") != -1) {//在跳跃
            status = "jump_right";
        } else {
            status = "move_right";
        }
    }


    /**
     * 向左边停止
     */
    public void leftStop() {
        xSpeed = 0;
        if (status.indexOf("jump") != -1) {//在跳跃
            status = "jump_left";
        } else {
            status = "stop_left";
        }
    }

    public void rightStop() {
        xSpeed = 0;
        if (status.indexOf("jump") != -1) {//在跳跃
            status = "jump_right";
        } else {
            status = "stop_right";
        }
    }

    /**
     * 跳跃
     */
    public void jump(){
        if(status.indexOf("jump")==-1){//非跳跃状态才能跳跃
            if(status.indexOf("left")!=-1){
                status = "jump_left";
            }else{
                status = "jump_right";
            }
            ySpeed = -30;
            upTime = 7;
        }
    }

    /**
     * 下落
     * 有两种下落情况： 跳跃下落，从障碍物上掉下来
     */
    public void fall(){
        if(status.indexOf("left")!=-1){
            status = "jump_left";
        }else{
            status = "jump_right";
        }
        ySpeed = 10;
    }


    @Override
    public void run() {
        while (true) {
            boolean onObstacle = false;//是否在障碍物上，false不是

            int mw = 100;
            int mh = 100;

            for (int i = 0; i < background.getObstacleList().size(); i++) {
                Obstacle ob = background.getObstacleList().get(i);
                /*
                判断是否位于障碍物上
                判断Y轴： obY==y+mh
                判断X轴： obX> x-mw && obX < x+mw
                 */
//                System.out.println((ob.getY()+10)+":"+(this.y+mh));
                if((ob.getY()+10>this.y+mh&&ob.getY()-10<y+mh)){//判断Y轴
//                    if(ob.getX()>this.x-mw&&ob.getX()<this.x+mw){//判断X轴
                    if((ob.getX()>x&&ob.getX()<x+mw)||
                            (ob.getX()+30>x&&ob.getX()+30<x+mw)||
                            (ob.getX()<x&&ob.getX()+30>x+mw)){
//                        System.out.println((ob.getX())+":"+(x-mw)+":"+(this.x+mw));
                        onObstacle = true;
                    }
                }
            }

            //进行马里奥跳跃动作
            if(onObstacle&&upTime==0){
                if(status.indexOf("left")!=-1){
                    if(xSpeed!=0){
                        status = "move_left";
                    }else{
                        status = "stop_left";
                    }
                }else{
                    if(xSpeed!=0){
                        status = "move_right";
                    }else{
                        status = "stop_right";
                    }
                }
            }else{//处于跳跃上升
                if(upTime!=0){
                    upTime--;
                }else {
                    fall();
                }
                y += ySpeed;
            }


            if (xSpeed < 0 || xSpeed > 0) {//判断是否在移动
                x += xSpeed;
                //判断马里奥是否移动到最左边
                if (x < 0) x = 0;
            }
            if (status.contains("move")) {//判断是否在移动
                index = index == 0 ? 1 : 0;
            }

            if ("move_left".equals(status)) {//左移
                show = StaticValue.run_L.get(index);//不断改变index，形成跑动动画
            } else if ("move_right".equals(status)) {
                show = StaticValue.run_R.get(index);//不断改变index，形成跑动动画
            }else if("stop_left".equals(status)){
                show = StaticValue.stand_L;
            }else if("stop_right".equals(status)){
                show = StaticValue.stand_R;
            }else if("jump_left".equals(status)){
                show = StaticValue.jump_L;
            }else if("jump_right".equals(status)){
                show = StaticValue.jump_R;
            }

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }
}
